// pages/my/my.js
import httpUtil from '../../utils/httpUtil.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
      login: 0,
      imgUrl: '',
      nickName: '',
      isshare: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
 
  },
  goodsOrder: function () {
    wx.navigateTo({
      url: '/pages/goodsOrder/goodsOrder'
    })

  },
  allOrder: function () {
    wx.navigateTo({
      url: '/pages/order/order'
    })

  },
  myCoupon: function () {
    wx.navigateTo({
      url: '/pages/myCoupon/myCoupon',
    })
  },
  myAddress: function () {
    console.log(wx.getStorageSync('userId'))
    wx.navigateTo({
      url: '/pages/myAddress/myAddress',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    //获取当前网络状态
    wx.getNetworkType({
      success: function (res) {
        // 返回网络类型, 有效值：
        // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
        if (res.networkType == 'none' || res.networkType == 'unknown') {
          that.setData({
            outline: true
          })
        } else {
          that.setData({
            outline: false,
          })
          if (wx.getStorageSync('imgUrl') != '' && wx.getStorageSync('nickName') != '') {
            that.setData({
              imgUrl: wx.getStorageSync('imgUrl'),
              nickName: wx.getStorageSync('nickName'),
              login: 1
            })
          } else {
            that.onGotUserInfo()
          }
        }
      }
    })

    //监听网络状态
    wx.onNetworkStatusChange(function (res) {
      if (res.networkType == 'none' || res.networkType == 'unknown') {
        that.setData({
          outline: true
        })
      } else {
        that.setData({
          outline: false,
        })
        if (wx.getStorageSync('imgUrl') != '' && wx.getStorageSync('nickName') != '') {
          that.setData({
            imgUrl: wx.getStorageSync('imgUrl'),
            nickName: wx.getStorageSync('nickName'),
            login: 1
          })
        } else {
          that.onGotUserInfo()
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  onGotUserInfo: function (e) {
    var that = this;
    if (e == undefined) {

    } else {
        var req = {
          openid: wx.getStorageSync("openId"),
          nickname: e.detail.userInfo.nickName,
          headimgurl: e.detail.userInfo.avatarUrl,
          sex: e.detail.userInfo.gender,
          province: e.detail.userInfo.province,
          city: e.detail.userInfo.city,
        }
        httpUtil.post("/crm/myuserinfo", req)
          .then(function (res) {
            if (res.data.status == 200) {
              wx.setStorageSync("imgUrl", res.data.data.headimgurl);
              wx.setStorageSync("nickName", res.data.data.nickname);
              wx.setStorageSync("userId", res.data.data.id);
              that.setData({
                imgUrl: e.detail.userInfo.avatarUrl,
                nickName: e.detail.userInfo.nickName,
                login: 1
              })
            } else {
              wx.showToast({
                title: '' + res.data.message + '',
              })
            }
          }).catch(function (res) {
            console.log(res);
          });
    }

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})